package com.epfresh.supplier.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.supplier.R;
import com.epfresh.supplier.dialog.OfferBuyerDialog;
import com.epfresh.supplier.entity.BuyerInfo;
import com.epfresh.supplier.entity.OfferListEntity;

import java.util.List;

/**
 * Created by zzq on 2018年4月23日14:49:54
 */
public class OfferHistoryAdapter extends BaseAdapter implements View.OnClickListener {

    public Context context;
    public List<OfferListEntity.ContentBean.ProductListBean> list;
    private AdapterView.OnItemClickListener onItemClickListener;

    public OfferHistoryAdapter(Context context, List<OfferListEntity.ContentBean.ProductListBean> list) {
        this.context = context;
        this.list = list;
    }

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_offer_history, null, false);
            vh.rl_store = convertView.findViewById(R.id.rl_store);
            vh.tv_offer_history_store = (TextView) convertView.findViewById(R.id.tv_offer_history_store);
            vh.tv_offer_history_tag_1 = (TextView) convertView.findViewById(R.id.tv_offer_history_tag_1);
            vh.tv_offer_history_tag_2 = (TextView) convertView.findViewById(R.id.tv_offer_history_tag_2);
            vh.iv_offer_history_state = (ImageView) convertView.findViewById(R.id.iv_offer_history_state);

            vh.iv_offer_history_product = (ImageView) convertView.findViewById(R.id.iv_offer_history_product);
            vh.tv_offer_history_title = (TextView) convertView.findViewById(R.id.tv_offer_history_title);
            vh.tv_offer_history_unit = (TextView) convertView.findViewById(R.id.tv_offer_history_unit);
            vh.tv_offer_history_count = (TextView) convertView.findViewById(R.id.tv_offer_history_count);
            vh.tv_offer_history_price_old = (TextView) convertView.findViewById(R.id.tv_offer_history_price_old);
            vh.tv_offer_history_price_new = (TextView) convertView.findViewById(R.id.tv_offer_history_price_new);
            vh.tv_offer_history_require = (TextView) convertView.findViewById(R.id.tv_offer_history_require);
            vh.v_line_bottom = convertView.findViewById(R.id.v_line_bottom);
            vh.tv_offer_history_time = (TextView) convertView.findViewById(R.id.tv_offer_history_time);
            vh.tv_offer_history_reason = (TextView) convertView.findViewById(R.id.tv_offer_history_reason);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        OfferListEntity.ContentBean.ProductListBean bean = list.get(position);
        ImageLoader.getInstance().displayImage(bean.getImageUrl(), vh.iv_offer_history_product);
        vh.tv_offer_history_title.setText(bean.getTitle());
        vh.tv_offer_history_unit.setText(bean.getPack());
        vh.tv_offer_history_count.setText("要货数量：" + bean.getCount() + bean.getChargeUnit());
/*
        if (bean.getMerMsg() == null || "".equals(bean.getMerMsg())) {
            vh.tv_offer_history_require.setVisibility(View.GONE);
        } else {
            vh.tv_offer_history_require.setVisibility(View.VISIBLE);
            vh.tv_offer_history_require.setText("采购要求：" + bean.getMerMsg());
        }*/

        if("原价".equals(bean.getPriceTitle())){
            vh.tv_offer_history_price_old.setText("原价："+bean.getPriceUnit());
            vh.tv_offer_history_price_new.setText("");
        }else {
            Log.e("_______","_________"+bean.getPriceTitle());
            vh.tv_offer_history_price_old.setText("报价：");
            vh.tv_offer_history_price_new.setText(bean.getPriceUnit());
        }


        vh.rl_store.setTag(R.id.item_key_position, position);
        vh.rl_store.setOnClickListener(this);
        if (bean.isHeader()) {
            vh.rl_store.setVisibility(View.VISIBLE);
            vh.tv_offer_history_store.setText(bean.getFather().getAccountName());
            vh.tv_offer_history_tag_1.setText(bean.getFather().getAccountTag());
            vh.tv_offer_history_tag_2.setText(bean.getFather().getAccountSubjectName());
            if (!TextUtils.isEmpty(bean.getFather().getAccountTag()))
                vh.tv_offer_history_tag_1.setVisibility(View.VISIBLE);
            else
                vh.tv_offer_history_tag_1.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(bean.getFather().getAccountSubjectName()))
                vh.tv_offer_history_tag_2.setVisibility(View.VISIBLE);
            else
                vh.tv_offer_history_tag_2.setVisibility(View.GONE);

            if (2 == bean.getFather().getOfferStatus()) {
                vh.iv_offer_history_state.setImageResource(R.mipmap.offer_quit);
            } else {
                vh.iv_offer_history_state.setImageResource(R.mipmap.offer_already);
            }
        } else {
            vh.rl_store.setVisibility(View.GONE);
        }
        if (bean.isFoot()) {
            vh.tv_offer_history_time.setVisibility(View.VISIBLE);
            vh.tv_offer_history_reason.setVisibility(View.VISIBLE);
            vh.v_line_bottom.setVisibility(View.VISIBLE);
            vh.tv_offer_history_time.setText(bean.getCreateTime());
            vh.tv_offer_history_reason.setText(bean.getFather().getRemark());
        } else {
            vh.v_line_bottom.setVisibility(View.GONE);
            vh.tv_offer_history_time.setVisibility(View.GONE);
            vh.tv_offer_history_reason.setVisibility(View.GONE);
        }

        return convertView;
    }

    private OfferBuyerDialog modifyDialog;
    String dialogId;
    BuyerInfo bean;

    @Override
    public void onClick(View v) {
        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            Integer position = (Integer) tag;
            switch (v.getId()) {
                case R.id.rl_store:
                    if (modifyDialog == null)
                        modifyDialog = new OfferBuyerDialog(context, true);
                    modifyDialog.show(list.get(position).getFather());
                    break;
            }
        }
    }

    public class ViewHolder {
        View rl_store, v_line_top, v_line_bottom;
        TextView tv_offer_history_store, tv_offer_history_tag_1, tv_offer_history_tag_2;
        ImageView iv_offer_right, iv_offer_history_state;
        TextView tv_offer_history_title, tv_offer_history_unit, tv_offer_history_count, tv_offer_history_price_old, tv_offer_history_price_new, tv_offer_history_require;
        ImageView iv_offer_history_product, iv_promotion;
        TextView tv_offer_history_time, tv_offer_history_reason;
    }
/*
    private RequestTag requestTag = new RequestTag();
    String cgiName = "order/offer/buyerOrderInfo";

    public void requestInfo(String id) {
        if (modifyDialog != null && dialogId != null && bean != null && dialogId.equals(id)) {
            modifyDialog.show(bean);
            return;
        }
        dialogId = id;
        RequestEntitySupply<Map<String, String>> entity = new RequestEntitySupply<>();
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        entity.setCmd(cgiName);
        entity.setParameters(map);
        requestTag.tag = "order/offer/buyerOrderInfo";
        ((BaseActivity) context).request(entity, requestTag, onRequestListener);
    }

    OnRequestListener<BuyerInfo> onRequestListener = new OnRequestListener<BuyerInfo>() {
        @Override
        public BuyerInfo jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, BuyerInfo.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            ((BaseActivity) context).hideProgressDialog();
            ((BaseActivity) context).toast("网络问题,请点击重试");
            modifyDialog.dismiss();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            ((BaseActivity) context).hideProgressDialog();
            ((BaseActivity) context).toast(arg + "");
            modifyDialog.dismiss();
        }

        @Override
        public void onStart(Object tag) {
            ((BaseActivity) context).showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<BuyerInfo> response, Object tag) {
            bean = response.getResponseElement();

            ((BaseActivity) context).hideProgressDialog();
            if (modifyDialog == null)
                modifyDialog = new OfferBuyerDialog(context, true);
            modifyDialog.show(bean);
        }
    };*/

}
